package com.example.drainage.ui.downwin

import android.content.res.ColorStateList
import android.view.View
import androidx.core.graphics.toColorInt
import com.example.drainage.base.ktexpand.visible
import com.example.drainage.databinding.ActivityDownWinBinding

/**
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.example.armmvvm.ui.downwin
 * @createTime 创建时间 ：2021/12/9
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
class DownWinHelper(private val binding: ActivityDownWinBinding) {

    private val timeDownWin = DownWinTimeType(binding)

    private val distanceDownWin = DownWinDistanceType(binding)

    private val districtDownWin = DownWinDistrictType(binding)

    private var curDownWin: DownWinType? = null

    var conditionChangeListener: ((month: String, district: String, distance: String, grantTimeSort: Boolean) -> Unit)? = null

    init {
        binding.apply {
            val colorStateList = ColorStateList(
                arrayOf(
                    intArrayOf(android.R.attr.state_selected),
                    intArrayOf(-android.R.attr.state_selected)
                ), intArrayOf(
                    "#FF1A7AFE".toColorInt(), "#FF2B2F36".toColorInt()
                )
            )
            ivTime.imageTintList = colorStateList
            ivDistrict.imageTintList = colorStateList
            ivDistance.imageTintList = colorStateList
            ivGrantTime.imageTintList = colorStateList
            tvTime.setTextColor(colorStateList)
            tvDistrict.setTextColor(colorStateList)
            tvDistance.setTextColor(colorStateList)
            tvGrantTime.setTextColor(colorStateList)

            topTabs.post {
                tvTime.maxWidth = (tvTime.parent as View).measuredWidth - ivTime.measuredWidth
                tvDistance.maxWidth = (tvDistance.parent as View).measuredWidth - ivDistance.measuredWidth
                tvDistrict.maxWidth = (tvDistrict.parent as View).measuredWidth - ivDistrict.measuredWidth
                tvGrantTime.maxWidth = (tvGrantTime.parent as View).measuredWidth - ivGrantTime.measuredWidth
            }

            (tvTime.parent as View).setOnClickListener {
                showDownWin(timeDownWin)
            }
            (tvDistance.parent as View).setOnClickListener {
                showDownWin(distanceDownWin)
            }
            (tvDistrict.parent as View).setOnClickListener {
                showDownWin(districtDownWin)
            }
            (tvGrantTime.parent as View).setOnClickListener {
                showDownWin(null)
                val isSelect = !ivGrantTime.isSelected
                ivGrantTime.isSelected = isSelect
                conditionChange()
            }

            tvReset.setOnClickListener {
                curDownWin?.reset()
            }
            tvEnsure.setOnClickListener {
                curDownWin?.ensure()
                conditionChange()
                showDownWin(null)
            }
            downWin.setOnClickListener {
                showDownWin(null)
            }

            // 假数据
            districtDownWin.setDefaultDistrict("天河区")
        }
    }

    private fun showDownWin(type: DownWinType?) {
        if (curDownWin == type) {
            return
        }
        curDownWin?.close()
        curDownWin = type
        curDownWin?.show()
        binding.downWin.visible = type != null
    }

    private fun conditionChange() {
        binding.apply {
            conditionChangeListener?.invoke(
                tvTime.text.toString(),
                tvDistrict.text.toString(),
                tvDistance.text.toString(),
                ivGrantTime.isSelected
            )
        }
    }

}